package com.suifeng.mq.order.controller;

import com.suifeng.mq.common.utils.UuidUtils;
import com.suifeng.mq.order.dao.MqMessageDao;
import com.suifeng.mq.order.entity.MqMessage;
import com.suifeng.mq.order.entity.OrderEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RabbitController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    private MqMessageDao mqMessageDao;

    @GetMapping("/sendMq")
    public String sendMq(@RequestParam(value = "num", defaultValue = "10") Integer num) {


        for (int i = 0; i < num; i++) {
            if (i % 2 == 0) {
                OrderEntity orderEntity = new OrderEntity();
                String uuid = UuidUtils.generateUuid();
                orderEntity.setOrderSn(uuid);
                orderEntity.setStatus(3);
                //同时将mq消息数据保存到mq_message表中 message_status  0-新建
                rabbitTemplate.convertAndSend("hello-java-exchange", "hello-java", orderEntity,new CorrelationData(uuid));

//                MqMessage mqMessage = new MqMessage();
//                mqMessage.setMessageId(uuid);
//                mqMessage.setClassType(orderEntity.getClass().toString());
//                mqMessage.setContent(JSON.toJSONString(orderEntity));
//                mqMessage.setMessageStatus("0");
//                mqMessage.setToExchange("hello-java-exchange");
//                mqMessage.setRoutingKey("hello-java");
//                mqMessage.setCreateTime(new Date());
//                mqMessage.setUpdateTime(new Date());
//                mqMessageDao.insert(mqMessage);

            } else {
                MqMessage mqMessage = new MqMessage();
                String uuid = UuidUtils.generateUuid();
                mqMessage.setMessageId(uuid);
                mqMessage.setContent("发送MQ消息");
                rabbitTemplate.convertAndSend("hello-java-exchange", "hello-java1", mqMessage,new CorrelationData(uuid));
            }

        }

        return "ok";
    }
}
